TOP 30 Java Interview Coding Tasks by Matthew Urban

TOP 30 Java Interview Coding Tasks by Matthew Urban

Author:Matthew Urban [Urban, Matthew]
Language: eng
Format: epub
ISBN: 9788365477088
Publisher: net-boss
Published: 2018-07-02T23:00:00+00:00


Listing 15.2 – All possible permutations of characters in String object (iterative).

public class StringUtils { public static List<String> permutation(String text){ List<String> permutations = new ArrayList<>(); List<String> result = new ArrayList<>(); result.add(text.substring(0,1)); int length = text.length(); for (int i=1; i < length; i++) { permutations.clear(); for (String perm : result) { permutations.addAll(addCharacter(text.charAt(i), perm)); } result.clear(); result.addAll(permutations); } return result; } private static Set<String> addCharacter(Character c, String text) { Set<String> set = new HashSet<>(); int length = text.length(); for (int i=0; i<= length; i++) { set.add(text.substring(0, i) + c + text.substring(i, length)); } return set; } }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.